package bean.circular.dependency.setter;

import org.junit.Assert;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Author com.alibaba.lizhen
 * @Date 2021/4/9 7:15 下午
 * @Version 1.0
 * @Description
 */
public class SetterTest {

	/**
	 * Spring的setter依赖注入解决。通过XML配置<property></property>。
	 */
	@Test
	public void testXML() {
		ApplicationContext context
				= new ClassPathXmlApplicationContext("bean.circular.dependency/setter.xml");
		A a = context.getBean("a", A.class);
		B b = context.getBean("b", B.class);
		Assert.assertEquals(a.getB(), b);
	}


	/**
	 * Spring通过Autowired注解。底层是反射
	 */
	@Test
	public void testAnnotation() {
		ApplicationContext context
				= new AnnotationConfigApplicationContext(Config.class);
		A a = context.getBean("a", A.class);
		B b = context.getBean("b", B.class);
		System.out.println(a.getB());
		System.out.println(b.getA());
		Assert.assertEquals(a.getB(), b);
	}
}
